package com.google.mr.okhttptest;

import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.support.design.widget.AppBarLayout;
import android.support.design.widget.CollapsingToolbarLayout;
import android.support.v7.widget.Toolbar;
import android.text.TextUtils;
import android.view.View;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.ProgressBar;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.google.mr.okhttptest.bean.ZhihuDetail;
import com.google.mr.okhttptest.net.Contants;
import com.google.mr.okhttptest.net.JsonGenericsSerializator;
import com.orhanobut.logger.Logger;
import com.zhy.http.okhttp.OkHttpUtils;
import com.zhy.http.okhttp.callback.GenericsCallback;

import butterknife.BindView;
import okhttp3.Call;

/**
 * Created by Administrator on 2017/11/29.
 */

public class StoryDetailActivity extends BaseActivity {

    private static final String STORY_ID = "STORY_ID";
    private static int mStoryId;
    @BindView(R.id.story_detail_image)
    ImageView storyDetailImage;
    @BindView(R.id.tv_imgBelong)
    TextView tv_imgBelong;
    @BindView(R.id.tv_detail_title)
    TextView tv_detail_title;
    @BindView(R.id.view_topcover)
    View viewTopcover;
    @BindView(R.id.story_detail_toolbar)
    Toolbar storyDetailToolbar;
    @BindView(R.id.story_detail_collapsingtoolbarlayout)
    CollapsingToolbarLayout storyDetailCollapsingtoolbarlayout;
    @BindView(R.id.story_detail_appbar)
    AppBarLayout storyDetailAppbar;
    @BindView(R.id.story_detail_webview)
    WebView storyDetailWebview;
    @BindView(R.id.ll_loading)
    FrameLayout ll_loading;
    @BindView(R.id.pb_loading)
    ProgressBar pb_loading;


    public static Intent getIntent(Context mContext, int storyid) {
        Logger.i("storyid:" + storyid);
        mStoryId = storyid;
        Intent intent = new Intent(mContext, StoryDetailActivity.class);
        intent.putExtra(STORY_ID, storyid);
        return intent;
    }

    @Override
    protected void initFeature() {

    }

    @Override
    protected int layoutId() {
        return R.layout.activity_storydetail;
    }

    @Override
    protected void initView() {
        setSupportActionBar(storyDetailToolbar);
        storyDetailCollapsingtoolbarlayout.setTitle(" ");
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
    }

    @Override
    protected void initListener() {
        storyDetailToolbar.setNavigationOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                onBackPressed();
            }
        });
    }

    @Override
    protected void initData() {
        if (mStoryId != -1) {
            OkHttpUtils
                    .get()
                    .url(Contants.URLZHIHU + mStoryId)
                    .id(100)
                    .build().execute(new GenericsCallback<ZhihuDetail>(new JsonGenericsSerializator()) {
                @Override
                public void onError(Call call, Exception e, int id) {
                    Logger.i("onError:call:" + call);
                }

                @Override
                public void onResponse(ZhihuDetail response, int id) {
                    Logger.i("response:" + response);
                    storyDetailToolbar.setTitle(response.getTitle());
                    if (!TextUtils.isEmpty(response.getImage())) {
                        Glide.with(storyDetailImage.getContext()).load(response.getImage()).into(storyDetailImage);
                        tv_imgBelong.setText(response.getImage_source());
                    } else {
                        storyDetailImage.setImageResource(R.mipmap.radio_5);
                    }
                    tv_detail_title.setText(response.getTitle());

                    LoadingWebView(response.getBody());
                }
            });
        }
    }

    private void LoadingWebView(String htmlContent) {
        WebSettings settings = storyDetailWebview.getSettings();
        settings.setJavaScriptEnabled(true);
        settings.setAppCacheEnabled(true);
        settings.setCacheMode(WebSettings.LOAD_CACHE_ELSE_NETWORK);
        settings.setAppCachePath(getDir("chache", MODE_PRIVATE).getPath());
        settings.setDomStorageEnabled(true);

        String css = "<link rel=\"stylesheet\" href=\"file:///android_asset/css/news.css\" type=\"text/css\">";
        String html = "<html><head>" + css + "</head><body>" + htmlContent + "</body></html>";
        html = html.replace("<div class=\"img-place-holder\">", "");

        storyDetailWebview.loadDataWithBaseURL("x-data://base", html, "text/html", "UTF-8", null);
        storyDetailWebview.setWebViewClient(new WebViewClient() {
            @Override
            public void onPageStarted(WebView view, String url, Bitmap favicon) {
                super.onPageStarted(view, url, favicon);
                pb_loading.setVisibility(View.VISIBLE);
            }

            //设置结束加载函数
            @Override
            public void onPageFinished(WebView view, String url) {
                pb_loading.setVisibility(View.GONE);
            }
        });
    }


    @Override
    public void onBackPressed() {
        super.onBackPressed();
        finish();
    }
}
